﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YJKArch.AreaStatistical.TableTools
{
	public class StringComposingKit
	{
		public StringComposingKit(string text, double rectangleWidth, double rectangleHeight)
		{
			this.m_Text = text;
			this.m_CharWidthList = this.GetCharWidthList(text);
			this.m_RectangleWidth = rectangleWidth;
			this.m_RectangleHeight = rectangleHeight;
		}

		public void ComosingHorizontalAndVertical(ref double fontHeight, ref double fontRate, ref List<string> mText)
		{
			double num = this.m_RectangleWidth / this.m_RectangleHeight;
			double num2 = 100.0;
			double num3 = 0.0;
			List<string> list = null;
			int i = 0;
			int num4 = 1;
			while (i < this.m_CharWidthList.Count)
			{
				double num5 = 0.5;
				for (int j = 0; j < 5; j++)
				{
					List<string> list2 = new List<string>();
					this.CalculateMText(num4, ref list2);
					double num6 = this.CalculateUsageAreaRate(num5, list2);
					double num7 = Math.Abs(num - num6);
					if (num7 < num2)
					{
						num2 = num7;
						num3 = num5;
						list = list2;
					}
					num5 += 0.1;
					if (num / num6 > 10.0)
					{
						goto IL_B8;
					}
				}
				num4++;
				i++;
			}
			IL_B8:
			fontRate = num3;
			mText = list;
			fontHeight = this.CalculateFontHeight(num, fontRate, mText);
		}

		public void ComosingVertical(double fontHeight, double fontRate, double HFrameRate, ref List<string> mText)
		{
			double length = (double)this.m_Text.Length;
			double num = fontHeight * fontRate;
			int rowsCount = (int)Math.Ceiling(length * num / (this.m_RectangleWidth - HFrameRate * num));
			this.CalculateMText(rowsCount, ref mText);
		}

		private List<int> GetCharWidthList(string str)
		{
			List<int> list = new List<int>();
			if (str.Length == 0)
			{
				return list;
			}
			byte[] bytes = new ASCIIEncoding().GetBytes(str);
			for (int i = 0; i < bytes.Length; i++)
			{
				list.Add(1);
			}
			return list;
		}

		private void CalculateMText(int rowsCount, ref List<string> mText)
		{
			int num = (int)Math.Ceiling((double)this.m_Text.Length * 1.0 / (double)rowsCount);
			int i = 0;
			int num2 = 0;
			int num3 = 0;
			while (i < this.m_CharWidthList.Count)
			{
				num2 += this.m_CharWidthList[i];
				if (num2 >= num || i == this.m_CharWidthList.Count - 1)
				{
					num2 = 0;
					string text = "";
					for (int j = num3; j <= i; j++)
					{
						text += this.m_Text[j].ToString();
					}
					mText.Add(text);
					num3 = i + 1;
				}
				i++;
			}
		}

		private double CalculateUsageAreaRate(double curFontRate, List<string> curMText)
		{
			double num = 10.0;
			double num2 = num * curFontRate;
			double num3 = 0.0;
			for (int i = 0; i < curMText.Count; i++)
			{
				double num4 = (double)curMText[i].Length * num2;
				if (num4 > num3)
				{
					num3 = num4;
				}
			}
			return num3 / ((double)curMText.Count * num);
		}

		private double CalculateFontHeight(double optimalRate, double fontRate, List<string> mText)
		{
			if (this.CalculateUsageAreaRate(fontRate, mText) > optimalRate)
			{
				int num = 0;
				for (int i = 0; i < mText.Count; i++)
				{
					int length = mText[i].Length;
					if (length > num)
					{
						num = length;
					}
				}
				return this.m_RectangleWidth / (double)num / fontRate;
			}
			return this.m_RectangleHeight / (double)mText.Count;
		}

		private double m_RectangleWidth;

		private double m_RectangleHeight;

		private string m_Text = "";

		private List<int> m_CharWidthList = new List<int>();
	}
}
